package com.lq.ecoldchain.ui.mine.carInfo.carline.add

import android.content.Intent
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.lq.ecoldchain.R
import com.lq.ecoldchain.base.BaseNavigator
import com.lq.ecoldchain.base.view.BaseActivity
import com.lq.ecoldchain.callback.SafeClickListener
import com.lq.ecoldchain.config.AppConfig
import com.lq.ecoldchain.databinding.ActivityAddLineBinding
import com.lq.ecoldchain.entity.City
import com.lq.ecoldchain.ui.mine.carInfo.carline.city.CityAllActivity
import kotlinx.android.synthetic.main.activity_add_line.*
import org.kodein.di.Kodein
import org.kodein.di.generic.instance

/*
*2019/1/2 0002  18:42
*function by lq
*/
@Route(path = BaseNavigator.addLine)
class AddLineActivity : BaseActivity<ActivityAddLineBinding>() {
    override val kodein = Kodein.lazy {
        extend(parentKodein)
        import(addLineKodeinModule)
    }

    override val layoutId = R.layout.activity_add_line
    override val viewDelegate: AddLineDelegate by instance()
    var isStart = true
    override fun initView() {
        binding.delegate = viewDelegate
        titleText("添加路线")
        initListener()
    }

    private fun initListener() {
        startRl.setOnClickListener(object : SafeClickListener() {
            override fun onNoDoubleClick(v: View) {
                isStart = true
                startActivityForResult(Intent(this@AddLineActivity, CityAllActivity::class.java), AppConfig.ADD_LINE_REQUEST_CODE)
            }
        })
        endRl.setOnClickListener(object : SafeClickListener() {
            override fun onNoDoubleClick(v: View) {
                isStart = false
                startActivityForResult(Intent(this@AddLineActivity, CityAllActivity::class.java), AppConfig.ADD_LINE_REQUEST_CODE)
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK && requestCode == AppConfig.ADD_LINE_REQUEST_CODE) {
            //获取
            val city = data?.getParcelableExtra<City>("data")
            if (isStart) {
                viewDelegate.viewModel.startPlace.value = city?.cityName
                viewDelegate.viewModel.startId.value = city?.id
            }else{
                viewDelegate.viewModel.endPlace.value = city?.cityName
                viewDelegate.viewModel.endId.value = city?.id
            }

        }
    }

}
